var Script404 = 
{
	/* Constants */
	CREATE			: "create",
	REVIEW			: "review",
	UPDATE			: "update",
	DELETE			: "delete",
	
	/* variable */
	url					: "",
	query 				: "",
	
	connect: function(callback, id)
	{
		//Send an HTTP request to establish connection to the database
		var ref = this;
		var conn;

		if (window.XMLHttpRequest)
			conn = new XMLHttpRequest();
		else
			conn = new ActiveXObject("Microsoft.XMLHTTP");
		
		conn.open("POST", ref.url, true);
		conn.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		conn.onreadystatechange = function ()
		{
			if (conn.readyState == 4 && conn.status == 200)
			{
				//alert (conn.responseText);

				//Callback function
				callback(ref, id, conn.responseText);	
			}
		};
		conn.send(ref.query);	
	},
	
	generateQuery: function(tag, content)
	{
		if (this.query.length == 0)
			this.query = tag + "=" + content;
		else
			this.query += "&" + tag + "=" + content;
		
	},

	
	hideCreate: function () {
		this.hide(this.CREATE);
	},
	
	hideReview: function () {
		this.hide(this.REVIEW);
	},
	
	hideUpdate: function () {
		this.hide(this.UPDATE);
	},
	
	hideDelete: function () {
		this.hide(this.DELETE);
	},
	
	hideAll: function () {
		this.hideCreate();
		this.hideReview();
		this.hideUpdate();
		this.hideDelete();
	},
	
	showCreate: function (collapsed) {
		if (collapsed) {
			this.hideAll();
		}
		this.show(this.CREATE);
		this.currentShow = this.CREATE;
	},
	
	showReview: function (collapsed) {
		if (collapsed) {
			this.hideAll();
		}
		this.show(this.REVIEW);
		this.currentShow = this.REVIEW;
	},
	
	showUpdate: function (collapsed) {
		if (collapsed) {
			this.hideAll();
		}
		this.show(this.UPDATE);
		this.currentShow = this.UPDATE;
	},
	
	showDelete: function (collapsed) {
		if (collapsed) {
			this.hideAll();
		}
		this.show(this.DELETE);
		this.currentShow = this;
	},
	
	showAll: function () {
		this.showCreate();
		this.showReview();
		this.showUpdate();
		this.showDelete();
	},
	
	initShow: function (feed) {
		if (feed == this.CREATE) {
			this.showCreate(true);
		}
		else if (feed == this.UPDATE) {
			this.showUpdate(true);
		}
		else if (feed == this.DELETE) {
			this.showDelete(true);
		}
		else {
			this.showReview(true);
		}
	},
	
	hide: function (id) {
		var element = document.getElementById(id);
		if (element != null) {
			element.style.display="none";
		}
	},
	
	show: function (id) {
		var element = document.getElementById(id);
		if (element != null) {
			element.style.display="inherit";
		}
	},
	
	courseDesc: function(value, destination) {
		this.url = "http://localhost:8080/LabAssistant/CourseDescription";
		
		this.query = "";
		this.generateQuery("courseName", value);
		
		this.connect(this.showDesc, destination);
	},
	
	accountDesc: function(value, destination) {
		this.url = "http://localhost:8080/LabAssistant/AccountUpdateDescription";
		
		this.query = "";
		this.generateQuery("username", value);
		
		this.connect(this.showDesc2, destination);
	},
	
	labDesc: function(value, destination) {
		this.url = "http://localhost:8080/LabAssistant/LabDescription";
		
		this.query = "";
		this.generateQuery("labId", value);
		
		this.connect(this.showDesc3, destination);
	},
	
	showDesc: function(ref, id, text) {
		var element = document.getElementById(id);
		element.innerHTML = "<strong>Description:</strong><br/>" + text; 
	},
	
	showDesc2: function(ref, id, text) {
		var arr = text.split(":");
		var nameValue = arr[0];
		var emailValue = arr[1];
		var element = document.getElementById(id);
		element.innerHTML = "<strong>Description:</strong><br/>Name:  " + nameValue + "<br/>Email: " + emailValue; 
	},
	
	showDesc3: function(ref, id, text) {
		var arr = text.split(":");
		var courseValue = arr[0];
		var startValue = arr[1];
		var endValue = arr[2];
		var assignmentValue = arr[3];
		var sizeValue = arr[4];
		var element = document.getElementById(id);
		element.innerHTML = "<strong>Description:</strong><br/>Course:  " + courseValue 
							+ "<br/>Start Date: " + startValue + "<br/>End Date: " + endValue 
							+ "<br/>Team Assignment: " + assignmentValue + "<br/>Team Size: " + sizeValue; 
	},
	
	accountUpdate: function(value, id) {
		this.url = "http://localhost:8080/LabAssistant/AccountUpdateDescription";
		
		this.query = "";
		this.generateQuery("username", value);
		
		this.connect(this.showAccountUpdate, id);
	},
	
	labUpdate: function(value, id) {
		this.url = "http://localhost:8080/LabAssistant/LabUpdateDescription";
		
		this.query = "";
		this.generateQuery("labId", value);
		
		this.connect(this.showLabUpdate, id);
	},
	
	showAccountUpdate: function(ref, form, value) {
		var formElement = document.forms[form];
		var arr = value.split(":");
		var nameValue = arr[0];
		var emailValue = arr[1];
		var statusValue = arr[2];
		
		formElement.name.value = nameValue;
		formElement.email.value = emailValue;
		
		if (statusValue == "Active") {
			formElement.status.selectedIndex = 1;
		}
		else {
			formElement.status.selectedIndex = 2;
		}
	},
	
	showLabUpdate: function (ref, form, value) {
		var formElement = document.forms[form];
		var arr = value.split(":");
		var name = arr[0];
		var startValue = arr[1];
		var endValue = arr[2];
		var assignment = arr[3];
		var size = arr[4];

		formElement.name.value = name;
		formElement.startDate.value = startValue;
		formElement.endDate.value = endValue;
		formElement.teamSize.selectedIndex = size - 1;
		
		if (assignment == "Individual") {
			formElement.teamAssignment.selectedIndex = 1;
		}
		else if (assignment == "Random") {
			formElement.teamAssignment.selectedIndex = 2;
		}
		else if (assignment == "Self Select") {
			formElement.teamAssignment.selectedIndex = 3;
		}
	}
};